package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextPane;

import net.miginfocom.swing.MigLayout;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;


public class ObjP extends JPanel {

	private JPanel photoNorth = new JPanel();
	private JPanel photoSouth = new JPanel();
	private JButton upload1N = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload2N = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload3N = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload4N = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));

	private String lastPath = "";
	private String pathIm1N = "";
	private String pathIm2N = "";
	private String pathIm3N = "";
	private String pathIm4N = "";
	private String sourceIm1N = "";
	private String sourceIm2N = "";
	private String sourceIm3N = "";
	private String sourceIm4N = "";

	private JButton upload1S = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload2S = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload3S = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload4S = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));

	private String pathIm1S = "";
	private String pathIm2S = "";
	private String pathIm3S = "";
	private String pathIm4S = "";
	private String sourceIm1S = "";
	private String sourceIm2S = "";
	private String sourceIm3S = "";
	private String sourceIm4S = "";

	private int fotoNorth = 0;
	private int fotoSouth = 0;

	private JPanel rilievi = new JPanel();
	private JTextPane puntoCritico = new JTextPane();
	private JTextPane possibileCausa = new JTextPane();
	private JTextPane possibileSoluzione = new JTextPane();

	private String directoryProject;
	private int p;

	private StartingWindow st;

	protected void paintComponent(Graphics g) {
		setOpaque(false);
		g.drawImage((new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg"))).getImage(), 0, 0, null);
		super.paintComponent(g);
	}

	public ObjP(final StartingWindow st, final int p) {
		super();
		this.p = p;
		this.st = st;
		Properties pc = System.getProperties();
		String homePath = pc.getProperty("user.home");
		directoryProject = homePath + pc.getProperty("file.separator") + "ArchimedeSuper" + pc.getProperty("file.separator") + "aaAAzzZZ"
				+ pc.getProperty("file.separator");
		lastPath = st.getPath();
		upload1N.setBorder(null);
		upload1N.setContentAreaFilled(false);
		upload1N.setRolloverEnabled(true);
		upload1N.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload1N.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				int returnValue = chooser.showOpenDialog(upload1N);

				if (returnValue == JFileChooser.APPROVE_OPTION) {
					String sourcePath = chooser.getSelectedFile().toString();

					st.setPath(sourcePath);
					lastPath = sourcePath;

					StringTokenizer tok = new StringTokenizer(sourcePath, ".");

					sourceIm1N = sourcePath;
					ImageIcon tmp = new ImageIcon(sourcePath);
					tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

					String destPath = directoryProject + "1pnorth" + p + "." + "jpg";
					pathIm1N = destPath;

					upload1N.setIcon(tmp);

					photoNorth.add(upload2N, "cell 1 0");
					fotoNorth++;
					st.refresh();
				}
			}
		});

		upload2N.setBorder(null);
		upload2N.setContentAreaFilled(false);
		upload2N.setRolloverEnabled(true);
		upload2N.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload2N.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				int returnValue = chooser.showOpenDialog(upload2N);

				if (returnValue == JFileChooser.APPROVE_OPTION) {
					String sourcePath = chooser.getSelectedFile().toString();
					st.setPath(sourcePath);
					lastPath = sourcePath;
					StringTokenizer tok = new StringTokenizer(sourcePath, ".");

					sourceIm2N = sourcePath;
					ImageIcon tmp = new ImageIcon(sourcePath);
					tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

					String destPath = directoryProject + "2pnorth" + p + "." + "jpg";
					pathIm2N = destPath;

					upload2N.setIcon(tmp);
					fotoNorth++;
					if (fotoSouth < 3)
						photoNorth.add(upload3N, "cell 2 0");

					st.refresh();
				}
			}
		});

		upload3N.setBorder(null);
		upload3N.setContentAreaFilled(false);
		upload3N.setRolloverEnabled(true);
		upload3N.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload3N.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (fotoSouth == 0) {
					JFileChooser chooser = new JFileChooser();
					chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
					ImagePreviewPanel preview = new ImagePreviewPanel();
					chooser.setAccessory(preview);
					chooser.addPropertyChangeListener(preview);
					lastPath = st.getPath();
					if (lastPath.equals(""))
						chooser.getCurrentDirectory();
					else
						chooser.setCurrentDirectory(new File(lastPath));
					int returnValue = chooser.showOpenDialog(upload3N);

					if (returnValue == JFileChooser.APPROVE_OPTION) {
						String sourcePath = chooser.getSelectedFile().toString();
						st.setPath(sourcePath);
						lastPath = sourcePath;
						StringTokenizer tok = new StringTokenizer(sourcePath, ".");

						sourceIm3N = sourcePath;
						ImageIcon tmp = new ImageIcon(sourcePath);
						tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

						String destPath = directoryProject + "3pnorth" + p + "." + "jpg";
						pathIm3N = destPath;

						upload3N.setIcon(tmp);
						photoSouth.removeAll();

						photoNorth.add(upload4N, "cell 3 0");
						fotoNorth++;
						st.refresh();
					}
				} else {
					JOptionPane.showMessageDialog(null, "Rimuovere foto sottostante");
				}
			}
		});

		upload4N.setBorder(null);
		upload4N.setContentAreaFilled(false);
		upload4N.setRolloverEnabled(true);
		upload4N.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload4N.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				int returnValue = chooser.showOpenDialog(upload4N);

				if (returnValue == JFileChooser.APPROVE_OPTION) {
					String sourcePath = chooser.getSelectedFile().toString();
					st.setPath(sourcePath);
					lastPath = sourcePath;

					sourceIm4N = sourcePath;
					ImageIcon tmp = new ImageIcon(sourcePath);
					tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

					String destPath = directoryProject + "4pnorth" + p + "." + "jpg";
					pathIm4N = destPath;

					fotoNorth++;
					upload4N.setIcon(tmp);

					st.refresh();
				}
			}
		});

		upload1S.setBorder(null);
		upload1S.setContentAreaFilled(false);
		upload1S.setRolloverEnabled(true);
		upload1S.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload1S.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				int returnValue = chooser.showOpenDialog(upload1S);

				if (returnValue == JFileChooser.APPROVE_OPTION) {
					String sourcePath = chooser.getSelectedFile().toString();

					st.setPath(sourcePath);
					lastPath = sourcePath;

					sourceIm1S = sourcePath;
					ImageIcon tmp = new ImageIcon(sourcePath);
					tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

					String destPath = directoryProject + "1psouth" + p + "." + "jpg";
					pathIm1S = destPath;

					upload1S.setIcon(tmp);
					fotoSouth++;
					photoSouth.add(upload2S, "cell 1 0");
					st.refresh();
				}
			}
		});

		upload2S.setBorder(null);
		upload2S.setContentAreaFilled(false);
		upload2S.setRolloverEnabled(true);
		upload2S.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload2S.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				int returnValue = chooser.showOpenDialog(upload2S);

				if (returnValue == JFileChooser.APPROVE_OPTION) {
					String sourcePath = chooser.getSelectedFile().toString();
					st.setPath(sourcePath);
					lastPath = sourcePath;
					StringTokenizer tok = new StringTokenizer(sourcePath, ".");

					sourceIm2S = sourcePath;
					ImageIcon tmp = new ImageIcon(sourcePath);
					tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

					String destPath = directoryProject + "2psouth" + p + "." + "jpg";
					pathIm2S = destPath;

					upload2S.setIcon(tmp);
					fotoSouth++;
					if (fotoNorth < 3)
						photoSouth.add(upload3S, "cell 2 0");

					st.refresh();
				}
			}
		});

		upload3S.setBorder(null);
		upload3S.setContentAreaFilled(false);
		upload3S.setRolloverEnabled(true);
		upload3S.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload3S.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (fotoNorth == 0) {
					JFileChooser chooser = new JFileChooser();
					chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
					ImagePreviewPanel preview = new ImagePreviewPanel();
					chooser.setAccessory(preview);
					chooser.addPropertyChangeListener(preview);
					lastPath = st.getPath();
					if (lastPath.equals(""))
						chooser.getCurrentDirectory();
					else
						chooser.setCurrentDirectory(new File(lastPath));
					int returnValue = chooser.showOpenDialog(upload3S);

					if (returnValue == JFileChooser.APPROVE_OPTION) {
						String sourcePath = chooser.getSelectedFile().toString();
						st.setPath(sourcePath);
						lastPath = sourcePath;

						sourceIm3S = sourcePath;
						ImageIcon tmp = new ImageIcon(sourcePath);
						tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

						String destPath = directoryProject + "3psouth" + p + "." + "jpg";
						pathIm3S = destPath;

						upload3S.setIcon(tmp);
						photoNorth.removeAll();
						photoSouth.add(upload4S, "cell 3 0");
						fotoSouth++;
					} else {
						JOptionPane.showMessageDialog(null, "Rimuovere foto soprastante");
					}
					st.refresh();
				}
			}
		});

		upload4S.setBorder(null);
		upload4S.setContentAreaFilled(false);
		upload4S.setRolloverEnabled(true);
		upload4S.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
		upload4S.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				int returnValue = chooser.showOpenDialog(upload4S);

				if (returnValue == JFileChooser.APPROVE_OPTION) {
					String sourcePath = chooser.getSelectedFile().toString();
					st.setPath(sourcePath);
					lastPath = sourcePath;

					sourceIm4S = sourcePath;
					ImageIcon tmp = new ImageIcon(sourcePath);
					tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

					String destPath = directoryProject + "4psouth" + p + "." + "jpg";
					pathIm4S = destPath;

					upload4S.setIcon(tmp);
					fotoSouth++;
					st.refresh();
				}
			}
		});

		ClipboardPopupMenu pcClip = ClipboardPopupMenu.installForComponent(puntoCritico);
		pcClip.addCopyFunction("Copia");
		pcClip.addCutFunction("Taglia");
		pcClip.addPasteFunction("Incolla");

		ClipboardPopupMenu psClip = ClipboardPopupMenu.installForComponent(possibileSoluzione);
		psClip.addCopyFunction("Copia");
		psClip.addCutFunction("Taglia");
		psClip.addPasteFunction("Incolla");

		ClipboardPopupMenu cb = ClipboardPopupMenu.installForComponent(possibileCausa);
		cb.addCopyFunction("Copia");
		cb.addCutFunction("Taglia");
		cb.addPasteFunction("Incolla");

		MouseAdapter ml1 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoNorth--;
						if (!pathIm2N.equals("")) {
							pathIm1N = pathIm2N.replace("2p", "1p");

							sourceIm1N = sourceIm2N;
							upload1N.setIcon(upload2N.getIcon());

							if (!pathIm3N.equals("")) {
								pathIm2N = pathIm3N.replace("3p", "2p");

								sourceIm2N = sourceIm3N;
								upload2N.setIcon(upload3N.getIcon());
								if (!pathIm4N.equals("")) {
									pathIm3N = pathIm4N.replace("4p", "3p");

									sourceIm3N = sourceIm4N;
									pathIm4N = "";

									sourceIm4N = "";
									upload3N.setIcon(upload4N.getIcon());
									upload4N.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
								} else {
									upload3N.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
									pathIm3N = "";

									sourceIm3N = "";
									try {
										photoNorth.remove(upload4N);
									} catch (Exception noRemove) {
										System.out.println("no remove");
									}
									photoSouth.add(upload1S, "cell 0 0");
								}
							} else {
								upload2N.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
								pathIm2N = "";

								sourceIm2N = "";
								try {
									photoNorth.remove(upload3N);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}

							}
						} else {
							pathIm1N = "";

							sourceIm1N = "";
							upload1N.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							try {
								photoNorth.remove(upload2N);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
						}
						st.refresh();
					}
				}
			}
		};
		upload1N.addMouseListener(ml1);
		MouseAdapter ml2 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoNorth--;
						if (!pathIm3N.equals("")) {
							pathIm2N = pathIm3N.replace("3p", "2p");

							sourceIm2N = sourceIm3N;
							upload2N.setIcon(upload3N.getIcon());
							if (!pathIm4N.equals("")) {
								pathIm3N = pathIm4N.replace("4p", "3p");

								sourceIm3N = sourceIm4N;
								pathIm4N = "";

								sourceIm4N = "";
								upload3N.setIcon(upload4N.getIcon());
								upload4N.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							} else {
								upload3N.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
								pathIm3N = "";

								sourceIm3N = "";
								try {
									photoNorth.remove(upload4N);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}
								photoSouth.add(upload1S, "cell 0 0");
							}
						} else {
							upload2N.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							pathIm2N = "";

							sourceIm2N = "";
							try {
								photoNorth.remove(upload3N);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}

						}
						st.refresh();
					}
				}
			}
		};
		upload2N.addMouseListener(ml2);
		MouseAdapter ml3 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoNorth--;
						if (!pathIm4N.equals("")) {
							pathIm3N = pathIm4N.replace("4p", "3p");

							sourceIm3N = sourceIm4N;
							pathIm4N = "";

							sourceIm4N = "";
							upload3N.setIcon(upload4N.getIcon());
							upload4N.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
						} else {
							upload3N.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							pathIm3N = "";

							sourceIm3N = "";
							try {
								photoNorth.remove(upload4N);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
							photoSouth.add(upload1S, "cell 0 0");
						}
						st.refresh();
					}

				}
			}
		};
		upload3N.addMouseListener(ml3);
		MouseAdapter ml4 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoNorth--;
						upload4N.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
						pathIm4N = "";

						sourceIm4N = "";
						st.refresh();
					}

				}
			}
		};
		upload4N.addMouseListener(ml4);

		MouseAdapter mlS1 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoSouth--;
						if (!pathIm2S.equals("")) {
							pathIm1S = pathIm2S.replace("2p", "1p");

							sourceIm1S = sourceIm2S;
							upload1S.setIcon(upload2S.getIcon());

							if (!pathIm3S.equals("")) {
								pathIm2S = pathIm3S.replace("3p", "2p");

								sourceIm2S = sourceIm3S;
								upload2S.setIcon(upload3S.getIcon());
								if (!pathIm4S.equals("")) {
									pathIm3S = pathIm4S.replace("4p", "3p");

									sourceIm3S = sourceIm4S;
									pathIm4S = "";

									sourceIm4S = "";
									upload3S.setIcon(upload4S.getIcon());
									upload4S.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
								} else {
									upload3S.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
									pathIm3S = "";

									sourceIm3S = "";
									try {
										photoSouth.remove(upload4S);
									} catch (Exception noRemove) {
										System.out.println("no remove");
									}
									photoNorth.add(upload1N, "cell 0 0");
								}
							} else {
								upload2S.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
								pathIm2S = "";

								sourceIm2S = "";
								try {
									photoSouth.remove(upload3S);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}
							}
						} else {
							pathIm1S = "";

							sourceIm1S = "";
							upload1S.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							try {
								photoSouth.remove(upload2S);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
						}
						st.refresh();
					}
				}
			}
		};
		upload1S.addMouseListener(mlS1);
		MouseAdapter mlS2 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoSouth--;
						if (!pathIm3S.equals("")) {
							pathIm2S = pathIm3S.replace("3p", "2p");

							sourceIm2S = sourceIm3S;
							upload2S.setIcon(upload3S.getIcon());
							if (!pathIm4S.equals("")) {
								pathIm3S = pathIm4S.replace("4p", "3p");

								sourceIm3S = sourceIm4S;
								pathIm4S = "";

								sourceIm4S = "";
								upload3S.setIcon(upload4S.getIcon());
								upload4S.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							} else {
								upload3S.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
								pathIm3S = "";

								sourceIm3S = "";
								try {
									photoSouth.remove(upload4S);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}
								photoNorth.add(upload1N, "cell 0 0");
							}
						} else {
							upload2S.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							pathIm2S = "";

							sourceIm2S = "";
							try {
								photoSouth.remove(upload3S);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}

						}
						st.refresh();
					}
				}
			}
		};
		upload2S.addMouseListener(mlS2);
		MouseAdapter mlS3 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoSouth--;
						if (!pathIm4S.equals("")) {
							pathIm3S = pathIm4S.replace("4p", "3p");

							sourceIm3S = sourceIm4S;
							pathIm4S = "";
							sourceIm4S = "";
							upload3S.setIcon(upload4S.getIcon());
							upload4S.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
						} else {
							upload3S.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
							pathIm3S = "";
							sourceIm3S = "";
							try {
								photoSouth.remove(upload4S);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
							photoNorth.add(upload1N, "cell 0 0");
						}
						st.refresh();
					}
				}
			}
		};
		upload3S.addMouseListener(mlS3);
		MouseAdapter mlS4 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoSouth--;
						upload4S.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
						pathIm4S = "";
						sourceIm4S = "";
						st.refresh();
					}

				}
			}
		};
		upload4S.addMouseListener(mlS4);

		photoNorth.setLayout(new MigLayout());
		photoNorth.setOpaque(false);
		photoNorth.add(upload1N, "cell 0 0");

		photoSouth.setLayout(new MigLayout());
		photoSouth.setOpaque(false);
		photoSouth.add(upload1S, "cell 0 0");

		rilievi.setLayout(new MigLayout());
		rilievi.setOpaque(false);
		JLabel critico = new JLabel("P." + p + ".1 Punto critico");
		critico.setFont(new Font("Arial", Font.BOLD, 20));
		JLabel causa = new JLabel("P." + p + ".2 Possibile causa");
		causa.setFont(new Font("Arial", Font.BOLD, 20));
		JLabel soluzione = new JLabel("P." + p + ".3 Possibile soluzione");
		soluzione.setFont(new Font("Arial", Font.BOLD, 20));

		puntoCritico.setFont(new Font("Arial", Font.PLAIN, 16));
		possibileCausa.setFont(new Font("Arial", Font.PLAIN, 16));
		possibileSoluzione.setFont(new Font("Arial", Font.PLAIN, 16));
		puntoCritico.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		possibileCausa.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		possibileSoluzione.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		puntoCritico.setPreferredSize(new Dimension(700, 200));
		possibileCausa.setPreferredSize(new Dimension(700, 200));
		possibileSoluzione.setPreferredSize(new Dimension(700, 200));

		rilievi.add(causa, "wrap");
		rilievi.add(possibileCausa, "wrap");
		rilievi.add(soluzione, "wrap");
		rilievi.add(possibileSoluzione, "wrap");

		JPanel criticoPanel = new JPanel();
		criticoPanel.setLayout(new MigLayout());
		criticoPanel.setOpaque(false);
		criticoPanel.add(critico, "dock north");
		criticoPanel.add(puntoCritico, "dock north");

		setLayout(new MigLayout("", "", "25[]10[]10[]10[]"));

		add(criticoPanel, " gapleft 15 ,wrap");
		add(photoNorth, " gapleft 15 , wrap");
		add(rilievi, " gapleft 15, wrap");

		add(photoSouth, " gapleft 15, wrap");

	}

	public String print() {
		String print = "";
		print = print + "\n#" + pathIm1N.replace("aaAAzzZZ", st.getProject());
		print = print + "\n#" + pathIm2N.replace("aaAAzzZZ", st.getProject());
		print = print + "\n#" + pathIm3N.replace("aaAAzzZZ", st.getProject());
		print = print + "\n#" + pathIm4N.replace("aaAAzzZZ", st.getProject());
		print = print + "\n#" + pathIm1S.replace("aaAAzzZZ", st.getProject());
		print = print + "\n#" + pathIm2S.replace("aaAAzzZZ", st.getProject());
		print = print + "\n#" + pathIm3S.replace("aaAAzzZZ", st.getProject());
		print = print + "\n#" + pathIm4S.replace("aaAAzzZZ", st.getProject());
		print = print + "\n#start";
		print = print + "\n" + puntoCritico.getText();
		print = print + "\n#END";
		print = print + "\n#start";
		print = print + "\n" + possibileCausa.getText();
		print = print + "\n#END";
		print = print + "\n#start";
		print = print + "\n" + possibileSoluzione.getText();
		print = print + "\n#END";
		print = print + "\n#STOP";
		return print;
	}

	public void setElement(String data) {
		Scanner c = new Scanner(data);
		// System.out.println("PPPPPPPPPPPPPPPPPPPPPPPP " + data);
		// photo north

		sourceIm1N = c.nextLine().substring(1);
		if (sourceIm1N.contains("pnorth")) {

			pathIm1N = directoryProject.replace("aaAAzzZZ", st.getProject()) + "1pnorth" + p + "." + "jpg";
			sourceIm1N = pathIm1N;
			upload1N.setIcon(new ImageIcon(pathIm1N));
			photoNorth.add(upload2N, "cell 1 0");
			fotoNorth++;
		} else
			sourceIm1N = "";

		sourceIm2N = c.nextLine().substring(1);
		if (sourceIm2N.contains("pnorth")) {
			pathIm2N = directoryProject.replace("aaAAzzZZ", st.getProject()) + "2pnorth" + p + "." + "jpg";
			sourceIm2N = pathIm2N;
			upload2N.setIcon(new ImageIcon(pathIm2N));
			photoNorth.add(upload3N, "cell 2 0");
			fotoNorth++;
		} else
			sourceIm2N = "";

		sourceIm3N = c.nextLine().substring(1);
		if (sourceIm3N.contains("pnorth")) {

			pathIm3N = directoryProject.replace("aaAAzzZZ", st.getProject()) + "3pnorth" + p + "." + "jpg";
			sourceIm3N = pathIm3N;
			upload3N.setIcon(new ImageIcon(pathIm3N));
			photoNorth.add(upload4N, "cell 3 0");
			fotoNorth++;
			photoSouth.removeAll();
		} else
			sourceIm3N = "";

		sourceIm4N = c.nextLine().substring(1);
		if (sourceIm4N.contains("pnorth")) {
			pathIm4N = directoryProject.replace("aaAAzzZZ", st.getProject()) + "4pnorth" + p + "." + "jpg";
			sourceIm4N = pathIm4N;
			upload4N.setIcon(new ImageIcon(pathIm4N));
			fotoNorth++;
		} else
			sourceIm4N = "";

		// photo south
		sourceIm1S = c.nextLine().substring(1);
		if (sourceIm1S.contains("psouth")) {

			pathIm1S = directoryProject.replace("aaAAzzZZ", st.getProject()) + "1psouth" + p + "." + "jpg";
			sourceIm1S = pathIm1S;
			upload1S.setIcon(new ImageIcon(pathIm1S));
			photoSouth.add(upload2S, "cell 1 0");
			fotoSouth++;
		} else
			sourceIm1S = "";

		sourceIm2S = c.nextLine().substring(1);
		if (sourceIm2S.contains("psouth")) {

			pathIm2S = directoryProject.replace("aaAAzzZZ", st.getProject()) + "2psouth" + p + "." + "jpg";
			sourceIm2S = pathIm2S;
			upload2S.setIcon(new ImageIcon(pathIm2S));
			photoSouth.add(upload3S, "cell 2 0");
			fotoSouth++;
		} else
			sourceIm2S = "";

		sourceIm3S = c.nextLine().substring(1);
		if (sourceIm3S.contains("psouth")) {

			pathIm3S = directoryProject.replace("aaAAzzZZ", st.getProject()) + "3psouth" + p + "." + "jpg";
			sourceIm3S = pathIm3S;
			upload3S.setIcon(new ImageIcon(pathIm3S));
			photoSouth.add(upload4S, "cell 3 0");
			fotoSouth++;
			photoNorth.removeAll();
		} else
			sourceIm3S = "";

		sourceIm4S = c.nextLine().substring(1);
		if (sourceIm4S.contains("psouth")) {

			pathIm4S = directoryProject.replace("aaAAzzZZ", st.getProject()) + "4psouth" + p + "." + "jpg";
			sourceIm4S = pathIm4S;
			upload4S.setIcon(new ImageIcon(pathIm4S));
			fotoSouth++;
		} else
			sourceIm4S = "";

		boolean stop = false;
		c.nextLine();
		String testo = c.nextLine();
		while (c.hasNextLine() && !stop) {
			String tmp = c.nextLine();
			if (tmp.contains("\t"))
				tmp.replace("\t", "     ");
			if (tmp.equalsIgnoreCase("#end")) {
				puntoCritico.setText(testo);
				stop = true;
			}
			testo = testo + "\n" + tmp;
		}

		stop = false;
		c.nextLine();
		testo = c.nextLine();
		while (c.hasNextLine() && !stop) {
			String tmp = c.nextLine();
			if (tmp.contains("\t"))
				tmp.replace("\t", "     ");
			if (tmp.equalsIgnoreCase("#end")) {
				possibileCausa.setText(testo);
				stop = true;
			}
			testo = testo + "\n" + tmp;
		}
		stop = false;
		c.nextLine();
		testo = c.nextLine();
		while (c.hasNextLine() && !stop) {
			String tmp = c.nextLine();
			if (tmp.contains("\t"))
				tmp.replace("\t", "     ");
			if (tmp.equalsIgnoreCase("#end")) {
				possibileSoluzione.setText(testo);
				stop = true;
			}
			testo = testo + "\n" + tmp;
		}

	}

	public void resize(File in, int w, int h, String formato, String out) throws IOException {
		Image newImg = ImageIO.read(in).getScaledInstance(w, h, Image.SCALE_SMOOTH);
		BufferedImage bim = new java.awt.image.BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		bim.createGraphics().drawImage(newImg, 0, 0, null);
		FileOutputStream fos = new FileOutputStream(out);
		ImageIO.write(bim, formato, fos);
		fos.close();
	}

	public boolean save() {
		boolean done = true;
		if (!sourceIm1N.equals("") && !sourceIm1N.equals(pathIm1N))
			try {
				resize(new File(sourceIm1N), 250, 200, "jpg", pathIm1N.replace("aaAAzzZZ", st.getProject()));
			} catch (Exception e) {
				System.out.println("1n override");
				sourceIm1N = "";
				pathIm1N = "";
				upload1N.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
				done = false;
				e.printStackTrace();
			}
		if (!sourceIm2N.equals("") && !sourceIm2N.equals(pathIm2N))
			try {
				resize(new File(sourceIm2N), 250, 200, "jpg", pathIm2N.replace("aaAAzzZZ", st.getProject()));
			} catch (Exception e1) {
				System.out.println("2n override");
				sourceIm2N = "";
				pathIm2N = "";
				upload2N.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
				done = false;
				e1.printStackTrace();
			}
		if (!sourceIm3N.equals("") && !sourceIm3N.equals(pathIm3N))
			try {
				resize(new File(sourceIm3N), 250, 200, "jpg", pathIm3N.replace("aaAAzzZZ", st.getProject()));
			} catch (Exception e) {
				System.out.println("3n override");
				sourceIm3N = "";
				pathIm3N = "";
				upload3N.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
				done = false;
				e.printStackTrace();
			}
		if (!sourceIm4N.equals("") && !sourceIm4N.equals(pathIm4N))
			try {
				resize(new File(sourceIm4N), 250, 200, "jpg", pathIm4N.replace("aaAAzzZZ", st.getProject()));
			} catch (Exception e) {
				System.out.println("4n override");
				sourceIm4N = "";
				pathIm4N = "";
				upload4N.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
				done = false;
				e.printStackTrace();
			}

		if (!sourceIm1S.equals("") && !sourceIm1S.equals(pathIm1S))
			try {
				resize(new File(sourceIm1S), 250, 200, "jpg", pathIm1S.replace("aaAAzzZZ", st.getProject()));
			} catch (Exception e) {
				System.out.println("1s override");
				sourceIm1S = "";
				pathIm1S = "";
				upload1S.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
				done = false;
				e.printStackTrace();
			}
		if (!sourceIm2S.equals("") && !sourceIm2S.equals(pathIm2S))
			try {
				resize(new File(sourceIm2S), 250, 200, "jpg", pathIm2S.replace("aaAAzzZZ", st.getProject()));
			} catch (Exception e) {
				System.out.println("2s override");
				sourceIm2S = "";
				pathIm2S = "";
				upload2S.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
				done = false;
				e.printStackTrace();
			}
		if (!sourceIm3S.equals("") && !sourceIm3S.equals(pathIm3S))
			try {
				resize(new File(sourceIm3S), 250, 200, "jpg", pathIm3S.replace("aaAAzzZZ", st.getProject()));
			} catch (Exception e) {
				System.out.println("3s override");
				sourceIm3S = "";
				pathIm3S = "";
				upload3S.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
				done = false;
				e.printStackTrace();
			}
		if (!sourceIm4S.equals("") && !sourceIm4S.equals(pathIm4S))
			try {
				resize(new File(sourceIm4S), 250, 200, "jpg", pathIm4S.replace("aaAAzzZZ", st.getProject()));
			} catch (Exception e) {
				System.out.println("4s override");
				sourceIm4S = "";
				pathIm4S = "";
				upload4S.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
				done = false;
				e.printStackTrace();
			}
		return done;
	}

	public void replace(String newName, String oldName) {
		sourceIm1N = pathIm1N;
		sourceIm2N = pathIm2N;
		sourceIm3N = pathIm3N;
		sourceIm4N = pathIm4N;
		pathIm1N = pathIm1N.replace(oldName, "aaAAzzZZ");
		pathIm2N = pathIm2N.replace(oldName, "aaAAzzZZ");
		pathIm3N = pathIm3N.replace(oldName, "aaAAzzZZ");
		pathIm4N = pathIm4N.replace(oldName, "aaAAzzZZ");

		sourceIm1S = pathIm1S;
		sourceIm2S = pathIm2S;
		sourceIm3S = pathIm3S;
		sourceIm4S = pathIm4S;

		pathIm1S = pathIm1S.replace(oldName, "aaAAzzZZ");
		pathIm2S = pathIm2S.replace(oldName, "aaAAzzZZ");
		pathIm3S = pathIm3S.replace(oldName, "aaAAzzZZ");
		pathIm4S = pathIm4S.replace(oldName, "aaAAzzZZ");
	}

	public void changeP(int newP) {
		pathIm1S = pathIm1S.replace("1psouth" + p, "1psouth" + newP);
		pathIm2S = pathIm2S.replace("2psouth" + p, "2psouth" + newP);
		pathIm3S = pathIm3S.replace("3psouth" + p, "3psouth" + newP);
		pathIm4S = pathIm4S.replace("4psouth" + p, "4psouth" + newP);

		pathIm1N = pathIm1N.replace("1pnorth" + p, "1pnorth" + newP);
		pathIm2N = pathIm2N.replace("2pnorth" + p, "2pnorth" + newP);
		pathIm3N = pathIm3N.replace("3pnorth" + p, "3pnorth" + newP);
		pathIm4N = pathIm4N.replace("4pnorth" + p, "4pnorth" + newP);

		p = newP;
	}
}
